/*
 * TierManagementBean.java
 */

package ejb;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateful
public class TierManagementBean implements
        ejb.TierManagementBeanRemote {
    @PersistenceContext()
    EntityManager em;
    StakeholderTierEntity stakeholderTier;


    public TierManagementBean() {}

    public boolean createTier(String name, int commissionPercent, double feeSubscription, double feeMarketIntelligence) {
        boolean status;
        stakeholderTier = new StakeholderTierEntity();
        status = stakeholderTier.create(name, commissionPercent, feeSubscription, feeMarketIntelligence);
        em.persist(stakeholderTier);
        return status;
    }

    public boolean deleteTier(String name) {
        stakeholderTier = em.find(StakeholderTierEntity.class, name);
        if (stakeholderTier == null) {
            return false;
        }
        else {                    
            em.remove(stakeholderTier);
            return true;
        }
    }

    public List viewTier(String name) {
        List stateList = new ArrayList();
        stakeholderTier = em.find(StakeholderTierEntity.class, name);
        stateList.add(stakeholderTier.getName());
        stateList.add(stakeholderTier.getCommissionPercent());
        stateList.add(stakeholderTier.getFeeSubscription());
        stateList.add(stakeholderTier.getFeeMarketIntelligence());
        return stateList;
    }

    public boolean modifyTier(String name, int commissionPercent, double feeSubscription, double feeMarketIntelligence) {
        stakeholderTier = em.find(StakeholderTierEntity.class, name);
        if (stakeholderTier == null) {
            return false;
        }
        else {
        stakeholderTier.setCommissionPercent(commissionPercent);
        stakeholderTier.setFeeMarketIntelligence(feeMarketIntelligence);
        stakeholderTier.setFeeSubscription(feeSubscription);
        return true;
        }
    }

    @Remove
    public void remove() {
        System.out.println("TierManagementBean:remove()");
    }

}